package cn.hellochaos.generator.controller.web;


import cn.hellochaos.generator.entity.CaipuList;
import cn.hellochaos.generator.entity.FenleiCaipu;
import cn.hellochaos.generator.entity.dto.CaipuDetailDto;
import cn.hellochaos.generator.entity.dto.ResultBean;
import cn.hellochaos.generator.entity.vo.FenLeiVo2;
import cn.hellochaos.generator.service.CaipuListService;
import cn.hellochaos.generator.service.FenleiCaipuService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 主表：菜谱列表 前端控制器
 * </p>
 *
 * @author test
 * @since 2021-01-23
 * @version v1.0
 */
@RestController("webCaipuController")
@RequestMapping("/caipu-list")
public class CaipuController {

    @Autowired
    private CaipuListService caipuListService;

    @Autowired
    private FenleiCaipuService fenleiCaipuService;
    /**
     * 查找最新的7条菜谱
     */
    @RequestMapping(value = "/list-new-7")
    public ResultBean<?> findNewList7() {

        return new ResultBean<List<CaipuList>>(caipuListService.findNewList7());
    }

    /**
     * 查找菜谱的详情信息
     */
    @RequestMapping(method = RequestMethod.GET, value = "/{id}")
    public ResultBean<?> getCaipuDetailById(@PathVariable("id") Integer id) {
        return new ResultBean<CaipuDetailDto>(caipuListService.getCaipuDetailById(id));
    }

    // 根据菜谱名称查找菜谱
    @RequestMapping(method = RequestMethod.GET, value = "/getCookbookByCookBookName")
    public ResultBean<?> getCookbookByCookBookName(@RequestParam(name = "cookbookName", required = true) String cookbookName) {
        return new ResultBean<List<CaipuList>>(caipuListService.getCookbookByCookBookName(cookbookName));
    }

    // 查找菜谱分类
    @RequestMapping(method = RequestMethod.GET, value = "/categoryList")
    public ResultBean<?> categoryList() {
        return new ResultBean<Page<FenleiCaipu>>(fenleiCaipuService.listFenleiCaipusByPage(0,0,""));
    }

    /**
     * 查找分类
     */
    @RequestMapping(value = "/getFenlei")
    public ResultBean<List<FenLeiVo2>> getFenlei() {

        return new ResultBean<List<FenLeiVo2>>(fenleiCaipuService.getCaipuFenLei());
    }
    /**
     * 根据分类查找与之对应的菜谱
     * @return
     */
    @RequestMapping(value = "/getCaipuByFenlei")
    public ResultBean<List<CaipuList>> getCaipuByFenlei(String category) {

        return new ResultBean<List<CaipuList>>(fenleiCaipuService.getCaipuByFenlei(category));
    }

    /*===========================================================================*/
    /**
    * 查询分页数据
    */
    @RequestMapping(method = RequestMethod.GET)
    public ResultBean<?> listByPage(@RequestParam(name = "page", defaultValue = "1") int page,
                                    @RequestParam(name = "pageSize", defaultValue = "10") int pageSize,
                                    @RequestParam(name = "factor", defaultValue = "") String factor) {
        return new ResultBean<>(caipuListService.listCaipuListsByPage(page, pageSize,factor));
    }


    /**
    * 根据id查询
    */
//    @RequestMapping(method = RequestMethod.GET, value = "/{id}")
    public ResultBean<?> getById(@PathVariable("id") Integer id) {
        return new ResultBean<>(caipuListService.getCaipuListById(id));
    }

    /**
    * 新增
    */
    @RequestMapping(method = RequestMethod.POST)
    public ResultBean<?> insert(@RequestBody CaipuList caipuList) {
        return new ResultBean<>(caipuListService.insertCaipuList(caipuList));
    }

    /**
    * 删除
    */
    @RequestMapping(method = RequestMethod.DELETE, value = "/{id}")
    public ResultBean<?> deleteById(@PathVariable("id") Integer id) {
        return new ResultBean<>(caipuListService.deleteCaipuListById(id));
    }

    /**
    * 修改
    */
    @RequestMapping(method = RequestMethod.PUT)
    public ResultBean<?> updateById(@RequestBody CaipuList caipuList) {
        return new ResultBean<>(caipuListService.updateCaipuList(caipuList));
    }
}
